package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.PlatformMerchantUser;
import com.tiancheng.onecode.mapper.PlatformMerchantUserMapper;
import com.tiancheng.onecode.model.auth.UserMerchantProfile;
import com.tiancheng.onecode.model.dto.PlatformMerchantUserDetailDTO;
import com.tiancheng.onecode.service.IPlatformMerchantUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: kellen
 * @Description:商户用户管理 service
 * @Date: create in 2024/12/9 18:22
 */
@Service
public class PlatformMerchantUserServiceImpl extends ServiceImpl<PlatformMerchantUserMapper, PlatformMerchantUser> implements IPlatformMerchantUserService {

    @Override
    public List<UserMerchantProfile> queryByUserId(String userId) {
        return this.baseMapper.queryByUserId(userId);
    }

    @Override
    public PlatformMerchantUserDetailDTO queryPlatformMerchantSuperAdmin(Long platformMerchantId) {
        final List<PlatformMerchantUser> list = this.list(new LambdaQueryWrapper<PlatformMerchantUser>().eq(PlatformMerchantUser::getPlatformMerchantId, platformMerchantId)
                .eq(PlatformMerchantUser::getIsSuperAdmin, 1).eq(PlatformMerchantUser::getUserStatus, 1));
        if(!CollectionUtils.isEmpty(list)){
            final PlatformMerchantUserDetailDTO entity = new PlatformMerchantUserDetailDTO();
            entity.setUserName(list.get(0).getMerchantUserName());
            entity.setPhone(list.get(0).getPhone());
            entity.setCreatedDt(list.get(0).getCreatedDt());
            entity.setPlatformMerchantId(list.get(0).getPlatformMerchantId());
            entity.setUserId(list.get(0).getUserId());
            entity.setMerchantUserName(list.get(0).getMerchantUserName());
            entity.setIsSuperAdmin(list.get(0).getIsSuperAdmin());
            entity.setUserStatus(list.get(0).getUserStatus());
            return entity;
        }
        return null;
    }

    @Override
    public Boolean addSuperAdmin(PlatformMerchantUser platformMerchantUser) {
        platformMerchantUser.setCreatedDt(LocalDateTime.now());
        return this.save(platformMerchantUser);
    }

    @Override
    public Boolean removeMerchantSuperAdmin(Long platformMerchantId) {
        this.remove(new LambdaQueryWrapper<PlatformMerchantUser>().eq(PlatformMerchantUser::getIsSuperAdmin,1)
                .eq(PlatformMerchantUser::getPlatformMerchantId,platformMerchantId));
        return true;
    }
}
